import React from 'react'
import useRequest from './request'

export const useApi = () => {
  const request = useRequest()

  const getData = () => {
    return request({
      url: '/home/getData'
    })
  }

  // 用户
  const getUser = params => {
    return request({
      url: '/user/getUser',
      method: 'get',
      params
    })
  }
  const addUser = data => {
    return request({
      url: '/user/addUser',
      method: 'POST',
      data
    })
  }
  const editUser = data => {
    return request({
      url: '/user/editUser',
      method: 'POST',
      data
    })
  }
  const deleteUser = data => {
    return request({
      url: '/user/deleteUser',
      method: 'POST',
      data
    })
  }

  //密码注册
  const register = data => {
    return request({
      url: 'http://localhost:89/api/register',
      method: 'POST',
      data
    })
  }
  // 密码登录
  const login = data => {
    return request({
      url: 'http://localhost:89/api/login',
      method: 'POST',
      data
    })
  }

  //获取非对称加密公钥
  const getRsaPublicKey = data => {
    return request({
      url: 'http://localhost:89/api/getRsaPublicKey',
      method: 'POST',
      data
    })
  }

  //将加密过的对称加密公钥传给服务端
  const postAsePublicKey = data => {
    return request({
      url: 'http://localhost:89/api/postAsePublicKey',
      method: 'POST',
      data
    })
  }

  // 获取手机验证码
  const getCode = data => {
    return request({
      url: 'http://localhost:89/api/getCode',
      method: 'POST',
      data
    })
  }

  //新闻
  //获取新闻分类
  const getArticleType = () => {
    return request({
      url: 'http://localhost:89/article/getArticleType'
    })
  }

  //管理员添加新闻分类
  const addArticleType = data => {
    return request({
      url: 'http://localhost:89/article/addArticleType',
      method: 'post',
      data
    })
  }

  //管理员修改新闻分类状态，0为正常，1为禁用
  const changeTypeStatus = data => {
    return request({
      url: 'http://localhost:89/article/changeTypeStatus',
      method: 'post',
      data
    })
  }

  //删除新闻分类
  const deleteType = data => {
    return request({
      url: 'http://localhost:89/article/deleteType',
      method: 'post',
      data
    })
  }

  //文件上传
  const uploads = data => {
    return request.post('http://localhost:89/upload', data.formData, {
      // 需要设置请求头，告诉服务器发送的是 FormData
      headers: {
        'Content-Type': 'multipart/form-data'
      },
      signal: data.signal
    })
  }
  //合并文件分片请求
  const mergeChunk = data => {
    return request.post('http://localhost:89/merge', data.data, {
      signal: data.signal
    })
  }

  //文件秒传，校验文件是否已穿过
  const verifyFile = data => {
    return request({
      url: 'http://localhost:89/verifyFile',
      method: 'post',
      data
    })
  }

  //新闻发布
  const publishArticle = data => {
    return request({
      url: 'http://localhost:89/article/publishArticle',
      method: 'post',
      data
    })
  }

  //获取个人发布的文章
  const getUserArticle = () => {
    return request({
      url: 'http://localhost:89/article/getUserArticle'
    })
  }

  //管理员获取所有文章
  const getAllArticle = () => {
    return request({
      url: 'http://localhost:89/article/getAllArticle'
    })
  }

  //管理员获取所有已发布的文章
  const getPublishArticle = () => {
    return request({
      url: 'http://localhost:89/article/getPublishArticle'
    })
  }

  //管理员修改文章状态,0为正常，1为待审核或者封禁,-1为删除
  const changeArticleStatus = data => {
    return request({
      url: 'http://localhost:89/article/changeArticleStatus',
      method: 'post',
      data
    })
  }

  //用户修改文章内容
  const changeArticleContent = data => {
    return request({
      url: 'http://localhost:89/article/changeArticleContent',
      method: 'post',
      data
    })
  }

  //用户获取个人信息
  const getMyInfor = () => {
    return request({
      url: 'http://localhost:89/user/getMyInfor'
    })
  }

  //生成订单
  const addOrder = data => {
    return request({
      url: 'http://localhost:89/vip/addOrder',
      method: 'post',
      data
    })
  }

  //提交订单
  const submitOrder = data => {
    return request({
      url: 'http://localhost:89/vip/submitOrder',
      method: 'post',
      data
    })
  }

  //支付
  const paying = data => {
    return request.post('http://localhost:89/vip/paying', data, {
      // 需要设置请求头
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
      }
    })
  }

  //判断支付状态
  const successPay = data => {
    return request.post('http://localhost:89/vip/successPay', data, {
      // 需要设置请求头
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
      }
    })
  }

  return {
    getData,
    getUser,
    addUser,
    editUser,
    deleteUser,
    register,
    login,
    getRsaPublicKey,
    postAsePublicKey,
    getCode,
    getArticleType,
    addArticleType,
    changeTypeStatus,
    deleteType,
    uploads,
    mergeChunk,
    verifyFile,
    publishArticle,
    getUserArticle,
    getAllArticle,
    getPublishArticle,
    changeArticleStatus,
    changeArticleContent,
    getMyInfor,
    addOrder,
    submitOrder,
    paying,
    successPay
  }
}

// export const getData = () => {
//   return request({
//     url: '/home/getData'
//   })
// }

// export const getUser = params => {
//   return request({
//     url: '/user/getUser',
//     method: 'get',
//     params
//   })
// }

// export const addUser = data => {
//   return request({
//     url: '/user/addUser',
//     method: 'POST',
//     data
//   })
// }

// export const editUser = data => {
//   return request({
//     url: '/user/editUser',
//     method: 'POST',
//     data
//   })
// }

// export const deleteUser = data => {
//   return request({
//     url: '/user/deleteUser',
//     method: 'POST',
//     data
//   })
// }

// export const register = data => {
//   return request({
//     url: 'http://localhost:80/api/register',
//     method: 'POST',
//     data
//   })
// }

// export const login = data => {
//   return request({
//     url: 'http://localhost:89/api/login',
//     method: 'POST',
//     data
//   })
// }
